package ch.bfh.abcmmh.view.detailview.streets;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;

import javax.swing.JLabel;
import javax.swing.JPanel;

import ch.bfh.abcmmh.controller.operating.LogicController;
import ch.bfh.abcmmh.view.GUIController;
import ch.bfh.abcmmh.view.detailview.controllers.Streetcontroller;
import ch.bfh.abcmmh.view.detailview.minipanels.SpeedLimitPanel;
import ch.bfh.abcmmh.view.detailview.minipanels.SpeedLimitPanelMouseListener;


public class StreetPanel extends JPanel{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private int height;
	private int width;
	private int streets=3;
	private int grassHeight=60;
	private int streetPanelHeight;
	private LogicController logicController;
	private Streetcontroller streetController;
	private GUIController guiController;
	private int[] streetMidely;
	//private ImagePanel imgPanelTop;
	private ImagePanel imgPanelBottom;
	private SpeedLimitPanelMouseListener sped50;
	private SpeedLimitPanelMouseListener sped80;
	private SpeedLimitPanelMouseListener sped140;
	private Line line;
	
	
	public StreetPanel(GUIController guiController){
		
		this.line=new Line();
		this.guiController=guiController;
		this.logicController=guiController.getLogicController();
		this.streets=logicController.getCountofStreets();
		this.height =guiController.getDetailStreetPanelHeight();
		this.width = guiController.getWidth();
		//this.imgPanelTop = new ImagePanel("resources/grass.jpg", width, grassHeight);
		this.imgPanelBottom = new ImagePanel("resources/grass.jpg", width, grassHeight);
		this.streetPanelHeight=(height-grassHeight*2)/streets;
		this.setStreetController(new Streetcontroller(this, logicController));
		
		this.sped50=new SpeedLimitPanelMouseListener(50,this);
		this.sped80=new SpeedLimitPanelMouseListener(80,this);
		this.sped140=new SpeedLimitPanelMouseListener(140,this);
		sped80.setLocation(50,0);
		sped140.setLocation(100,0);
		line.setLocation(0, grassHeight);
		
		this.setBackground(Color.DARK_GRAY);
		this.setPreferredSize(new Dimension(width, height));
		this.setSize(new Dimension(width, height));
		this.setLocation(new Point(0, 0));
		this.setName("Mainframe");
		this.setLayout(null);
		this.setOpaque(true);
		this.streetMidely=new int[streets];
		setup();
		repaint();
		this.setVisible(true);
		
		
	
	}
	
	private void setup() {
		this.add(sped50);
		this.add(sped80);
		this.add(sped140);
		this.add(line);
		//imgPanelTop.setLocation(0, 0);
		//add(imgPanelTop);
		imgPanelBottom.setLocation(0,height-grassHeight);
		add(imgPanelBottom);
		addLines();
		setMidley();
		
	
		}
	public int streetHeight(){
		
		return (height-(2*grassHeight))/streets;
	}
	public void addSpeedLimit(int maxSpeed, int xPosition){
		
		SpeedLimitPanel spedP = new SpeedLimitPanel(maxSpeed, xPosition, this);
		spedP.setLocation(xPosition - streetController.getCurrentPos(), 0);
		spedP.repaint();
		this.add(spedP);
				
		repaintSubsandMe();
		
		
	
	
		
	}
	private void addWhitline(int y){
	
		int x=0;
		while (x<width) {
			WhiteLines whiteLine = new WhiteLines(streets);
			whiteLine.setLocation(x, y);
			add(whiteLine);
			x+=2*grassHeight/streets;}
		
		
	}
	
	private void addLines(){
		
		int y = grassHeight +streetPanelHeight;
		
		for(int i=1;i<streets;i++){
			
			addWhitline(y);
			y=y+ streetPanelHeight;

		}
		
		
	}
	
	private void setMidley(){
		
		int y = grassHeight + streets *streetPanelHeight-(streetPanelHeight/2);
		for(int i=0;i<streets;i++){
			
			streetMidely[i]=y;
			
			y=y-streetPanelHeight;
		}
		
	}

	
	private class WhiteLines extends JLabel{
		
		
		
		private static final long serialVersionUID = 1L;
		int x = 20;
		int y = 4;
		private WhiteLines(int Streets){
			x=20;
			y=12/streets;
			Dimension dimension = new Dimension(x,y);
			this.setPreferredSize(dimension);
			this.setSize(dimension);
			this.setOpaque(true);
			this.setVisible(true);
		}
		
		public void paintComponent(Graphics g){
			Graphics2D g2 = (Graphics2D) g;
			g2.setColor(new Color(255, 255, 255));
			g2.fillRect(0, 0, x, y);}


		
										}
private class Line extends JLabel{
		
		
		
		private static final long serialVersionUID = 1L;
		int x;
		int y;
		private Line(){
			x=1400;
			y=5;
			Dimension dimension = new Dimension(x,y);
			this.setPreferredSize(dimension);
			this.setSize(dimension);
			this.setOpaque(true);
			this.setVisible(true);
		}
		
		public void paintComponent(Graphics g){
			Graphics2D g2 = (Graphics2D) g;
			g2.setColor(Color.YELLOW);
			g2.fillRect(0, 0, x, y);}


		
										}


	public void repaintSubsandMe() {
		repaint();
		
	}
	public int getStreetMidley(int streetnumber){
		
		return streetMidely[streetnumber];
	}
	public LogicController getLogicController(){
		return logicController;
	}
	public int getWidth(){
		return width;
	}
	public int[] getSteetMidley(){
		return this.streetMidely;
	}
	public GUIController getGUIController(){
		return guiController;
	}

	public Streetcontroller getStreetController() {
		return streetController;
	}

	public void setStreetController(Streetcontroller streetController) {
		this.streetController = streetController;
	}
}
